package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GuoxueVideoActivity;
import com.xgkj.diyiketang.activity.GuoxueVideoTwoActivity;
import com.xgkj.diyiketang.activity.VideoDetailActivity;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沐诺 on 2017/4/7.
 * 更多里精彩视频 适配器
 */

public class VideoLiveVAdapter extends BaseAdapter {
    private Context mContext;
    private List<VideoInfo> mList = new ArrayList<VideoInfo>();

    public VideoLiveVAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<VideoInfo> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_videolivev, null);
            convertView.setTag(new ViewHolder(convertView));
        }

        VideoInfo info = mList.get(position);
        if (info != null) {
            initializeViews(info, (VideoLiveVAdapter.ViewHolder) convertView.getTag());
        }
        return convertView;
    }

    private void initializeViews(final VideoInfo info, VideoLiveVAdapter.ViewHolder holder) {
        String vidoeIconUrl = info.getVideoLogo();
        if (vidoeIconUrl != null && vidoeIconUrl.length() > 0) {
            ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 145), ViewUtil.dip2px(mContext, 84));
            String cache_key = ImageCache.getInstance(mContext).getCacheKey(vidoeIconUrl, size);
            Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cache_key);
            if (cacheBitmap != null) {
                holder.videoImg.setImageBitmap(cacheBitmap);
            } else {
                holder.videoImg.setImageResource(R.drawable.zhanweitu);
                holder.videoImg.setTag(vidoeIconUrl);
                ImageCache.getInstance(mContext).loadBitmapToView(holder.videoImg, vidoeIconUrl, null);
            }
        } else {
            holder.videoImg.setImageResource(R.drawable.zhanweitu);
        }

        holder.videoTime.setText(info.getVideoDuration());
        holder.videoTitle.setText(info.getVideoTitle());
        holder.school.setText(info.getSchoolName());
        holder.grade.setText(info.getBelongGrade()+"年级");
        holder.videoZan.setText(info.getZanNum());
        if(!"null".equals(info.getShangNum())){
            holder.videoHua.setText(info.getShangNum());
        }else{
            holder.videoHua.setText("0");
        }
        if(!"null".equals(info.getZanNum())){
            holder.videoZan.setText(info.getZanNum());
        }else{
            holder.videoZan.setText("0");
        }
        holder.videoTagImg.setVisibility(View.VISIBLE);
        int viewType = info.getViewType();
        if (viewType == 1) {
            holder.videoTagImg.setImageResource(R.drawable.vip);
        } else if (viewType == 2) {
            holder.videoTagImg.setImageResource(R.drawable.shoufei);
        } else {
            holder.videoTagImg.setVisibility(View.GONE);
        }

        holder.item_videolive_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uId = info.getUserId();
                String videoId = info.getVideoId();
                String youkuVideoId = info.getPath();
                String isuse = info.getIsuse();
                Intent intent = null;
                if (!TextUtils.isEmpty(info.getIsMoive())){
                    String isMovie = info.getIsMoive();
                    if (isMovie.equals("0")){
                         intent = new Intent(mContext, VideoDetailActivity.class);
                    }else {
                        intent = new Intent(mContext, GuoxueVideoTwoActivity.class);
                    }
                    intent.putExtra("isMovie",isMovie);
                }

                intent.putExtra("videoid", videoId);
                intent.putExtra("FEE", info.getFee());
                intent.putExtra("UID", uId);
                intent.putExtra("isUse",isuse);
                intent.putExtra("YOUKUVIDEOID", youkuVideoId);
                intent.putExtra("ZUOZHE", info.getUserName());
                mContext.startActivity(intent);

            }
        });
    }

    protected class ViewHolder {
        private ImageView videoImg;
        private ImageView videoTagImg;
        private TextView videoTime;
        private TextView videoTitle;
        private TextView school;
        private TextView grade;
        private TextView videoHua;
        private TextView videoZan;
        private RelativeLayout rootLayout,item_videolive_layout;

        public ViewHolder(View view) {
            rootLayout = (RelativeLayout) view.findViewById(R.id.item_videolive_rel);
            item_videolive_layout = (RelativeLayout) view.findViewById(R.id.item_videolive_layout);
            videoImg = (ImageView) view.findViewById(R.id.item_videolive_img);
            videoTagImg = (ImageView) view.findViewById(R.id.item_videolive_tag_img);
            videoTime = (TextView) view.findViewById(R.id.item_videolive_time);
            videoTitle = (TextView) view.findViewById(R.id.item_videolive_title);
            school = (TextView) view.findViewById(R.id.item_videolive_school);
            grade = (TextView) view.findViewById(R.id.item_videolive_grade);
            videoHua = (TextView) view.findViewById(R.id.item_videolive_gift);
            videoZan = (TextView) view.findViewById(R.id.item_videolive_laud);
        }
    }
}